package com.tfswx.ruleengine.compute.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.tfswx.ruleengine.compute.util.sm4.DecryptSm4InputStream;
import com.tfswx.ruleengine.compute.util.sm4.EncryptSm4InputStream;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
 * @author ricky
 * @date 2022/4/21 21:28
 */
public class FileUtil {
    /**
     * 读取文件到字符串
     *
     * @param file
     * @return
     * @throws IOException
     */
    static String readFileToString(File file) throws IOException {
        byte[] bytes = readFile(file);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    /**
     * 读取文件到字节数组
     *
     * @param file
     * @return
     * @throws IOException
     */
    static byte[] readFile(File file) throws IOException {
        try (DecryptSm4InputStream is = new DecryptSm4InputStream(Files.newInputStream(file.toPath())); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            IOUtils.copy(is, byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        }
    }

    /**
     * 将src加密到file
     *
     * @param file
     * @param src
     * @throws IOException
     */
    static void writeToFile(File file, File src) throws IOException {
        try (EncryptSm4InputStream encryptSm4InputStream = new EncryptSm4InputStream(Files.newInputStream(src.toPath())); FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            IOUtils.copy(encryptSm4InputStream, fileOutputStream);
        }
    }

    /**
     * 将bytes加密到file
     *
     * @param file
     * @param bytes
     * @throws IOException
     */
    static void writeToFile(File file, byte[] bytes) throws IOException {
        try (EncryptSm4InputStream encryptSm4InputStream = new EncryptSm4InputStream(bytes); FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            IOUtils.copy(encryptSm4InputStream, fileOutputStream);
        }
    }

    public static void writeGzbToFile(File file, Object o) throws IOException {
        String s = "gzyqgzb" + JSON.toJSONString(o, SerializerFeature.DisableCircularReferenceDetect) + "gzyqgzb";
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        writeToFile(file, bytes);
    }

    public static String readGzbFromFile(File file) throws IOException {
        String s = readFileToString(file);
        if (!s.startsWith("gzyqgzb") || !s.endsWith("gzyqgzb")) {
            throw new RuntimeException("文件不是规则包文件");
        }
        return s.substring(7, s.length() - 7);
    }

    public static void main(String[] args) throws IOException {
        // String s = DigestUtils.md5DigestAsHex("规则引擎加密密钥".getBytes());
        // System.out.println(s.toUpperCase());
        File file = new File("gzyqgzb.txt");
        // Map<String, String> map = new HashMap<>();
        // map.put("test", "测试的");
        // map.put("bj", "这个就是标记数据");
        // writeGzbToFile(file, map);
        String s = readGzbFromFile(file);
        System.out.println(s);

    }
}
